//
//  PeopleTopCollectionViewCell.m
//  markmap
//
//  Created by 甘世清 on 2024/8/14.
//

#import "PeopleTopCollectionViewCell.h"
#import "VipPopView.h"
#import "PeopleWXView.h"
@implementation PeopleTopCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.clipsToBounds = YES;
        [self SQ_UIViewForSelfView];
    }
    return self;
}
/// 添加ui
-(void)SQ_UIViewForSelfView
{
    UIImageView * bgImageView = [[UIImageView alloc]init];
    bgImageView.image = SQGetImage(@"peopleBg");
    bgImageView.contentMode = UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
    
    UIView * bgView = [[UIView alloc]init];
    bgView.backgroundColor = SQColor(@"#4A4A4A");
    bgView.layer.cornerRadius = 40;
    bgView.layer.masksToBounds = YES;
    [self.contentView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.height.mas_equalTo(100);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(40);
    }];
    UILabel * label1 = [[UILabel alloc]init];
    label1.text = SQNSLocalString(@"我的资料");
    label1.font = FONT_MEDIUM(15);
    label1.textColor = UIColor.whiteColor;
    [bgView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView).offset(16);
            make.top.equalTo(bgView).offset(24);
    }];
    [self.contentView addSubview:self.headerView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.sexImage];
    [self.contentView addSubview:self.rzImage];
    [self.contentView addSubview:self.guanzhuBtn];
    
    [self.contentView addSubview:self.huozhan];
    [self.contentView addSubview:self.fenshi];
    [self.contentView addSubview:self.guanzhu];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(88);
        make.left.equalTo(self.contentView).offset(11);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-130);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.headerView.mas_right).offset(10);
        make.top.equalTo(self.headerView).offset(16);
        make.height.mas_equalTo(28);
    }];
    
    [self.sexImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(16);
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.left.equalTo(self.nameLabel.mas_right).offset(6);
    }];
   
    [self.rzImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(16);
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.left.equalTo(self.sexImage.mas_right).offset(6);
    }];
    
    [self.guanzhuBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(88);
        make.height.mas_equalTo(32);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-180);
        make.right.equalTo(self.contentView.mas_right);
    }];
    [self.huozhan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.left.equalTo(self.contentView).offset(10);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-93);
    }];
    [self.fenshi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.left.equalTo(self.huozhan.mas_right).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-93);
    }];
    [self.guanzhu mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
        make.left.equalTo(self.fenshi.mas_right).offset(0);
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-93);
    }];
    WS(weakSelf);
    [self.guanzhuBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
            [weakSelf SQ_guanzhu];
    }];
    NSArray * array = @[@"获赞",@"粉丝",@"关注"];
    for (int x = 0; x<array.count; x++) {
        UILabel * la = [[UILabel alloc]init];
        la.text = SQNSLocalString([array objectAtIndex:x]);
        la.textAlignment = NSTextAlignmentCenter;
        la.font = FONT_REGULAR(15);
        la.textColor = SQColor333333;
        [self.contentView addSubview:la];
        [la mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(80);
            make.left.equalTo(self.contentView).offset(80*x+10);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-69);
        }];
    }
    [self.contentView addSubview:self.wxBtn];
    [self.wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-69);
            make.width.mas_equalTo(100);
            make.top.equalTo(self.huozhan).offset(10);
            make.right.equalTo(self.contentView.mas_right).offset(-10);
    }];
    [self.wxBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_popWXView];
    }];
    
    
    [self.collectionView registerClass:[PeopleDetailCell class] forCellWithReuseIdentifier:@"PeopleDetailCell"];
    self.collectionView.alwaysBounceVertical = NO;
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel);
        make.top.equalTo(self.nameLabel.mas_bottom);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.contentView.mas_right);
    }];
}
- (void)setMeModel:(PeopleDetaileModel *)meModel{
    _meModel = meModel;
    if ([self.meModel.userid isEqualToString:[UserModel share].userid]) {
        self.guanzhuBtn.hidden = YES;
    } else {
        self.guanzhuBtn.hidden = NO;
    }
    
    //头像
    [self.headerView.headerImage sd_setImageWithURL:SQURLImage(meModel.portrait) placeholderImage:SQGetImage(@"me背景")];
    self.nameLabel.text = meModel.name;
    self.wxBtn.hidden = YES;
    if (meModel.gender ==1) {
        self.sexImage.image = SQGetImage(@"男");
    } else {
        self.sexImage.image = SQGetImage(@"女");
        if ([meModel.wx isNotEmpty]) {
            self.wxBtn.hidden = NO;
        }
    }
    if (meModel.realStatus == 2) {
        self.rzImage.image = SQGetImage(@"认证");
    } else {
        self.rzImage.image = SQGetImage(@"未认证");
    }
    self.huozhan.text = [NSString stringWithFormat:@"%d",self.meModel.likeNum];
    self.fenshi.text = [NSString stringWithFormat:@"%d",self.meModel.fenCount];
    self.guanzhu.text = [NSString stringWithFormat:@"%d",self.meModel.attentionCount];
    self.jsonArray = [[NSMutableArray alloc]init];
    if ([self.meModel.age  isNotEmpty]) {
        [self.jsonArray addObject:self.meModel.age];
    }
    if ([self.meModel.city isNotEmpty]) {
        [self.jsonArray addObject:self.meModel.city];
    }
    if ([self.meModel.starSign isNotEmpty]) {
        [self.jsonArray addObject:self.meModel.starSign];
    }
    if ([self.meModel.weight isNotEmpty]&&[self.meModel.height isNotEmpty]) {
        [self.jsonArray addObject:[NSString stringWithFormat:@"%@cm/%@kg",self.meModel.height,self.meModel.weight]];
    } else if ([self.meModel.weight isNotEmpty]){
        [self.jsonArray addObject:[NSString stringWithFormat:@"%@kg",self.meModel.weight]];
    }else if ([self.meModel.height isNotEmpty]){
        [self.jsonArray addObject:[NSString stringWithFormat:@"%@cm",self.meModel.height]];
    }
    [self.collectionView reloadData];
    [self SQ_setGuanzhuBtn];  //是否关注
}
-(void)SQ_guanzhu{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    [dic setObject:self.meModel.userid forKey:@"pid"];
    if (self.meModel.isAttention) {
        [dic setObject:@"0" forKey:@"type"];
    } else {
        [dic setObject:@"1" forKey:@"type"];
    }
    [HttpTool post:Url38 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        if (self.meModel.isAttention) {
            self.meModel.fenCount --;
        } else {
            self.meModel.fenCount ++;
        }
        self.fenshi.text = [NSString stringWithFormat:@"%d",self.meModel.fenCount];
        self.meModel.isAttention = !self.meModel.isAttention;
        [self SQ_setGuanzhuBtn];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

-(void)SQ_setGuanzhuBtn{
    if (self.meModel.isAttention) {
        self.guanzhuBtn.text = SQNSLocalString(@"已关注");
        self.guanzhuBtn.imageView.hidden = YES;
    } else {
        self.guanzhuBtn.text = SQNSLocalString(@"关注");
        self.guanzhuBtn.imageView.hidden = NO;
    }
}

-(void)SQ_headerViewButtonClick{
    YBIBImageData * data = [YBIBImageData new];
    data.imageURL = SQURLImage(self.meModel.portrait);
    data.projectiveView = self.headerView;
    YBImageBrowser * brow = [YBImageBrowser new];
    brow.dataSourceArray = @[data];
    brow.currentPage =  0;
    [brow show];
}
-(void)SQ_popWXView{
    if ([UserModel share].isVip) {
        PeopleWXView * vc = [[PeopleWXView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200)];
        LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                                 parentView:nil
                                                   popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
        pop.hemStyle = LSTHemStyleBottom;
        
        [pop pop];
        LSTPopViewWK(pop)
        [pop setBgClickBlock:^{
            [wk_pop dismiss];
        }];
        vc.chatLabel.text = self.meModel.wx;
    } else {
        [VipPopView SQ_showVipPopView];
    }
}

#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    PeopleDetailCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PeopleDetailCell" forIndexPath:indexPath];
    cell.nameLabel.text = [self.jsonArray objectAtIndex:indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString* name = [self.jsonArray objectAtIndex:indexPath.item];
    UILabel * la = [[UILabel alloc]init];
    la.text = name;
    la.font = FONT_REGULAR(12);
    CGSize size = [la getLableSizeWithMaxWidth:200];
    return CGSizeMake(size.width+20, 25);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 14);
}


- (UIImageView *)rzImage{
    if (!_rzImage) {
        _rzImage = [[UIImageView alloc]init];
        _rzImage.image = SQGetImage(@"已认证");
        _rzImage.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _rzImage;
}
- (UIImageView *)sexImage{
    if (!_sexImage) {
        _sexImage = [[UIImageView alloc]init];
        _sexImage.image = SQGetImage(@"男");
        _sexImage.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _sexImage;
}
- (HeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[HeaderView alloc]init];
        _headerView.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        _headerView.clipsToBounds = YES;
        [_headerView.btn addTarget:self action:@selector(SQ_headerViewButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _headerView.layer.cornerRadius = 44;
        _headerView.layer.masksToBounds = YES;
    }
    return _headerView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = FONT_MEDIUM(20);
        _nameLabel.textColor = UIColor.blackColor;
        _nameLabel.backgroundColor = UIColor.clearColor;
    }
    return _nameLabel;
}
- (SQCustomButton *)guanzhuBtn{
    if (!_guanzhuBtn) {
        _guanzhuBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(12, 12) midmargin:4];
        _guanzhuBtn.SQBackimageView.image = SQGetImage(@"guanzhuBg");
        _guanzhuBtn.text = SQNSLocalString(@"关注");
        _guanzhuBtn.font = FONT_MEDIUM(14);
        _guanzhuBtn.image = SQGetImage(@"guanzhu5");
        _guanzhuBtn.textColor = UIColor.whiteColor;
        _guanzhuBtn.backgroundColor = UIColor.clearColor;
    }
    return _guanzhuBtn;
}
- (SQCustomButton *)wxBtn{
    if (!_wxBtn) {
        _wxBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(14, 14) midmargin:4];
        _wxBtn.SQBackimageView.image = SQGetImage(@"矩形");
        _wxBtn.text = SQNSLocalString(@"查看微信");
        _wxBtn.image = SQGetImage(@"wchat");
        _wxBtn.layer.cornerRadius = 20;
        _wxBtn.layer.masksToBounds = YES;
        _wxBtn.font = FONT_REGULAR(13);
        _wxBtn.textColor = UIColor.whiteColor;
        _wxBtn.backgroundColor = UIColor.clearColor;
    }
    return _wxBtn;
}


- (UILabel *)fenshi{
    if (!_fenshi) {
        _fenshi = [[UILabel alloc]init];
        _fenshi.textColor = SQColor333333;
        _fenshi.text = SQNSLocalString(@"0");
        _fenshi.font = FONT_MEDIUM(18);
        _fenshi.textAlignment = NSTextAlignmentCenter;
        
    }
    return _fenshi;
}
- (UILabel *)guanzhu{
    if (!_guanzhu) {
        _guanzhu = [[UILabel alloc]init];
        _guanzhu.textColor = SQColor333333;
        _guanzhu.text = SQNSLocalString(@"0");
        _guanzhu.font = FONT_MEDIUM(18);
        _guanzhu.textAlignment = NSTextAlignmentCenter;
    }
    return _guanzhu;
}
- (UILabel *)huozhan{
    if (!_huozhan) {
        _huozhan = [[UILabel alloc]init];
        _huozhan.textColor = SQColor333333;
        _huozhan.text = SQNSLocalString(@"0");
        _huozhan.font = FONT_MEDIUM(18);
        _huozhan.textAlignment = NSTextAlignmentCenter;
    }
    return _huozhan;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.userInteractionEnabled = YES;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.alwaysBounceVertical = YES;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

-(NSMutableArray*)jsonArray
{
    if (!_jsonArray) {
        _jsonArray = [[NSMutableArray alloc]init];
    }
    return _jsonArray;
}

@end



@implementation PeopleDetailCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.layer.masksToBounds = YES;
        self.clipsToBounds = YES;
        [self SQ_UIViewForSelfView];
    }
    return self;
}
/// 添加ui
-(void)SQ_UIViewForSelfView
{
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
    }];
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = FONT_REGULAR(12);
        _nameLabel.layer.cornerRadius = 4;
        _nameLabel.layer.masksToBounds = YES;
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.textColor = UIColor.whiteColor;
        _nameLabel.backgroundColor = SQHexColor(@"#000000", 0.5);
    }
    return _nameLabel;
}
@end
